/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import core.ProcessOwnerCore;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Dapstructure;
import model.Department;
import model.Stepvalidation;

/**
 *
 * @author phinguyen
 */
public class ServletAddStepToStructure extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        RequestDispatcher rd;
        HttpSession session = request.getSession();

        PrintWriter out = response.getWriter();
        ProcessOwnerCore poc = new ProcessOwnerCore();
        Dapstructure dapstructure = (Dapstructure) session.getAttribute("dapstructure");
        List<Stepvalidation> allStepOfDAP = new ArrayList();

        System.out.println("dapstructure : " + dapstructure);
        String[] steptitle = request.getParameterValues("steptitle_txt");

        System.out.println("steptitle : " + Arrays.toString(steptitle));
        String[] department = request.getParameterValues("department_txt");

        System.out.println("department : " + Arrays.toString(department));

        int nbstep = (int) session.getAttribute("nbstep");
        for (int i = 0; i < nbstep; i++) {
            int nbcheckbox = i + 1;
            String rb = "rollback_txt" + nbcheckbox;
            System.out.println("rolbacktext: " + rb);
            String[] rollback = request.getParameterValues(rb);

            if (i == 0) {
                Stepvalidation currentStepvalidation = poc.saveStep(steptitle[0], poc.getdepepartment(department[0]));
                allStepOfDAP.add(currentStepvalidation);
                System.out.println(currentStepvalidation.getSvid() - currentStepvalidation.getSvid());
            } else {
                if (rollback != null) {
                    System.out.println(Arrays.toString(rollback) + "tableau rollback : ");
                    Stepvalidation currentStepvalidation = poc.saveStep(steptitle[i], poc.getdepepartment(department[i]));
                    allStepOfDAP.add(currentStepvalidation);
                    System.out.println("new Stepvalidation id : " + currentStepvalidation.getSvid());
                    //System.out.println("rollback length : " + rollback.length);
                    System.out.println(rollback.length);
                    for (int j = 0; j < rollback.length; j++) {
                        System.out.println("dadaz");
                        int z = currentStepvalidation.getSvid() - i + new Integer(rollback[j]);
                        System.out.println(currentStepvalidation.getSvid() - i + new Integer(rollback[j]));
                        poc.addStepDap(dapstructure.getDsid(), currentStepvalidation.getSvid(), z, i);
                    }
                } else {
                    Stepvalidation currentStepvalidation = poc.saveStep(steptitle[i], poc.getdepepartment(department[i]));
                    allStepOfDAP.add(currentStepvalidation);
                    System.out.println(currentStepvalidation.getSvid() - currentStepvalidation.getSvid());
                }

            }
        }
//         System.out.println("rollback : " + Arrays.toString(rollback));
//        for (int i = 0; i < nbstep; i++) {
//            Stepvalidation currentStepvalidation = poc.saveStep(steptitle[i], poc.getdepepartment(department[i]));
//            System.out.println("new Stepvalidation id : " + currentStepvalidation.getSvid()); 
//            
//            poc.addStepDap(dapstructure.getDsid(), currentStepvalidation.getSvid(), currentStepvalidation.getSvid() - new Integer(rollback[i]), i);
//        }

//            Department dep = new Department(department[i]);
//        poc.saveStep(steptitle,poc.getdepepartment(department) );
//        Dapstructure dap= (Dapstructure) session.getAttribute("dapstructure");
//        poc.getStepbytitle(steptitle);
//        poc.addStepDap(1, step.getSvid(), 1, 1);
        request.setAttribute("allStepOfDAP", allStepOfDAP);
        rd = request.getRequestDispatcher("addStepDS2.jsp");
        rd.forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
